// var o;
// exports.PayConfig = void 0;
// var AL_GAME_HUA_i18nMgr = require("./AL_GAME_HUA_i18nMgr");
// var AL_GAME_HUA_Common = require("./AL_GAME_HUA_Common");
// var AL_GAME_HUA_Define = require("./AL_GAME_HUA_Define");
// var AL_GAME_HUA_EmailData = require("./AL_GAME_HUA_EmailData");
// var AL_GAME_HUA_PayData = require("./AL_GAME_HUA_PayData");
// var f = (function (e) {
//     function t() {
//         return (null !== e && e.apply(this, arguments)) || this;
//     }
//     __extends(t, e);
//     t.getInstance = function () {
//         return this.instance || (this.instance = new t());
//     };
//     t.prototype.destroy = function () {
//         t.instance = null;
//     };
//     t.prototype.jiazai_peizhi = function () {
//         return __awaiter(this, void 0, void 0, function () {
//             var e;
//             var t;
//             var n;
//             var o;
//             var i;
//             var r;
//             var s;
//             var c;
//             var l;
//             var u;
//             return __generator(this, function (a) {
//                 switch (a.label) {
//                     case 0:
//                         return [4, this.yibu_jiazai_json_peizhi("Pay")];
//                     case 1:
//                         if ((e = a.sent())) {
//                             for (t in ((this.payConf = {}), (this.payInfo = {}), e))
//                                 for (n = e[t], this.payConf[t] = [], o = 0; o < n.length; o++) {
//                                     i = n[o];
//                                     r = i.rewardType;
//                                     s = [];
//                                     for (c = 0; c < r.length; c += 2) {
//                                         l = r[c];
//                                         u = r[c + 1];
//                                         s.push({
//                                             goodsId: l,
//                                             num: u
//                                         });
//                                     }
//                                     i.rewardType = s;
//                                     this.payConf[t].push({
//                                         id: i.id,
//                                         action_id: i.payId,
//                                         productName: i.name,
//                                         productDesc: i.desc,
//                                         price: 100 * i.price,
//                                         currency: i.currency,
//                                         count: 1,
//                                         value: [],
//                                         rewardType: i.rewardType
//                                     });
//                                 }
//                             this.chushihua_zhifu_shuju();
//                         }
//                         return [2];
//                 }
//             });
//         });
//     };
//     t.prototype.chushihua_zhifu_shuju = function () {
//         var e;
//         e =
//             "native" == ailin_sdk_manager.getSysPlatformName()
//                 ? "native_" + ailin_sdk_manager.huoquYuanshengXitongXiangguanXinxi()
//                 : ailin_sdk_manager.getSysPlatformName();
//         this.payValid = e in this.payConf;
//         if (this.payValid) {
//             var t = this.payConf[e];
//             window.lieyou_PayData = this.payConf;
//             var n = 0;
//             for (var o = t; n < o.length; n++) {
//                 var i = o[n];
//                 this.payInfo[i.id] = i;
//             }
//             if (!this.huoqu_zhifu_shifou_youxiao()) {
//                 console.log("----支付未开启----");
//                 return void (this.payValid = !1);
//             }
//             window.LY_getPayData = function (e) {
//                 var n = {};
//                 var o = t;
//                 for (var i = 0; i < o.length; i++) {
//                     if (o[i].action_id == e) {
//                         (n = o[i]).value = "";
//                         break;
//                     }
//                 }
//                 return n.action_id
//                     ? (AL_GAME_HUA_Common.Common.shifou_tiaoshi_zhifu() && (n.price = 100), n)
//                     : (console.log("========未找到叮叮当===", n), null);
//             };
//             window.cpp_call_by_lost = function () {};
//         } else {
//             console.log("----不存在当前渠道配置，关闭支付----", e);
//         }
//     };
//     t.prototype.huoqu_zhifu_xinxi = function () {
//         return this.payInfo;
//     };
//     t.prototype.huoqu_zhifu_xinxi_by_id = function (e) {
//         return this.payInfo[e];
//     };
//     t.prototype.huoqu_zhifu_xinxi_by_action_id = function (e) {
//         for (var t in this.payInfo)
//             if (this.payInfo[t].action_id == e) {
//                 return this.payInfo[t];
//             }
//         return null;
//     };
//     t.prototype.huoqu_zhifu_shifou_youxiao = function () {
//         return !(
//             !this.payValid ||
//             (!AL_GAME_HUA_Common.Common.isDebug() &&
//                 (cc.sys.os == cc.sys.OS_IOS
//                     ? 1 == ailin_sdk_manager.instance.shifou_dakai_zhifu_ios
//                     : ailin_sdk_manager.shifouZhifuGoumai()))
//         );
//     };
//     t.prototype.huoqu_zhifu_shifou_keyi = function (e) {
//         return !(
//             !this.huoqu_zhifu_shifou_youxiao() ||
//             (!this.payInfo[e] && (console.log("----不存在当前支付配置----orderId:" + e), 1))
//         );
//     };
//     t.prototype.zhixing_zhifu = function (e) {
//         if (!this.huoqu_zhifu_shifou_keyi(e)) {
//             return Promise.resolve(null);
//         }
//         var t = this.payInfo[e];
//         AL_APP_SDK_MANAGER.showLoading({
//             timeout: 5
//         });
//         console.log("----发起支付----action_id:" + t.action_id + " orderId:" + t.id + ",price:" + t.price);
//         return new Promise(function (e) {
//             ailin_sdk_manager.diaoyongZhifuJiekou(Number(t.action_id), function (n) {
//                 console.log("----支付结果----", n);
//                 n
//                     ? (AL_GAME_HUA_PayData.PayData.getInstance().yichu_guanggao(),
//                       AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_PAY_SUCCESS, t),
//                       e(t),
//                       AL_APP_SDK_MANAGER.hideLoading())
//                     : (e(null), AL_APP_SDK_MANAGER.hideLoading());
//             });
//         });
//     };
//     t.prototype.jiancha_dingdan = function () {
//         var e = this;
//         return new Promise(function (t) {
//             if (!e.huoqu_zhifu_shifou_youxiao()) {
//                 return t(null);
//             }
//             ailin_sdk_manager.huoquYonghuDingdanLiebiao({
//                 callFun: function (e) {
//                     console.log("----获取订单----", e);
//                     if (!e.data) {
//                         console.error("----获取订单失败----");
//                         return t(null);
//                     }
//                     console.log("异常的订单:", e.data);
//                     var n = AL_GAME_HUA_EmailData.EmailData.getInstance();
//                     var o = n.huoqu_suoyou_youjian();
//                     for (var i = 0; i < e.data.length; i++) {
//                         var r = !1;
//                         for (var a = 0; a < o.length; a++) {
//                             if (o[a].id == e.data[i].orderSn) {
//                                 r = !0;
//                                 break;
//                             }
//                         }
//                         if (!r) {
//                             n.tianjia_youjian({
//                                 id: e.data[i].orderSn,
//                                 actionId: e.data[i].actionId,
//                                 type: 1,
//                                 createTime: AL_GAME_HUA_Common.Common.huoqu_dangqian_shijian(),
//                                 title: AL_GAME_HUA_i18nMgr.i18nMgr._huoqu_biaoqian("补单标题"),
//                                 validTime: 60
//                             });
//                         }
//                     }
//                     return t(e.data);
//                 }
//             });
//         });
//     };
//     t.instance = null;
//     return t;
// })(require("./AL_GAME_HUA_ConfigHandler").ConfigHandler);
// exports.PayConfig = f;




// var o;
// exports.PayConfig = void 0;
// var i18nMgr = require("./AL_GAME_HUA_i18nMgr");
// var Common = require("./AL_GAME_HUA_Common");
// var Define = require("./AL_GAME_HUA_Define");
// var EmailData = require("./AL_GAME_HUA_EmailData");
// var PayData = require("./AL_GAME_HUA_PayData");

// var PayConfig = (function (ConfigHandler) {
//     function PayConfig() {
//         return ConfigHandler !== null && ConfigHandler.apply(this, arguments) || this;
//     }
    
//     __extends(PayConfig, ConfigHandler);
    
//     PayConfig.getInstance = function () {
//         return this.instance || (this.instance = new PayConfig());
//     };

//     PayConfig.prototype.destroy = function () {
//         PayConfig.instance = null;
//     };

//     PayConfig.prototype.jiazai_peizhi = function () {
//         return __awaiter(this, void 0, void 0, function () {
//             var configData;
//             return __generator(this, function (_a) {
//                 switch (_a.label) {
//                     case 0:
//                         return [4, this.yibu_jiazai_json_peizhi("Pay")];
//                     case 1:
//                         configData = _a.sent();
//                         if (configData) {
//                             this.payConf = {};
//                             this.payInfo = {};
//                             for (var key in configData) {
//                                 var configArray = configData[key];
//                                 this.payConf[key] = [];
//                                 for (var i = 0; i < configArray.length; i++) {
//                                     var item = configArray[i];
//                                     var rewardArray = [];
//                                     for (var j = 0; j < item.rewardType.length; j += 2) {
//                                         rewardArray.push({
//                                             goodsId: item.rewardType[j],
//                                             num: item.rewardType[j + 1]
//                                         });
//                                     }
//                                     item.rewardType = rewardArray;
//                                     this.payConf[key].push({
//                                         id: item.id,
//                                         action_id: item.payId,
//                                         productName: item.name,
//                                         productDesc: item.desc,
//                                         price: 100 * item.price,
//                                         currency: item.currency,
//                                         count: 1,
//                                         value: [],
//                                         rewardType: item.rewardType
//                                     });
//                                 }
//                             }
//                             this.chushihua_zhifu_shuju();
//                         }
//                         return [2];
//                 }
//             });
//         });
//     };

//     PayConfig.prototype.chushihua_zhifu_shuju = function () {
//         var platformName = "native" === ailin_sdk_manager.getSysPlatformName()
//             ? "native_" + ailin_sdk_manager.huoquYuanshengXitongXiangguanXinxi()
//             : ailin_sdk_manager.getSysPlatformName();

//         this.payValid = platformName in this.payConf;

//         if (this.payValid) {
//             var payArray = this.payConf[platformName];
//             window.lieyou_PayData = this.payConf;
//             for (var i = 0; i < payArray.length; i++) {
//                 var payItem = payArray[i];
//                 this.payInfo[payItem.id] = payItem;
//             }

//             if (!this.huoqu_zhifu_shifou_youxiao()) {
//                 console.log("----支付未开启----");
//                 this.payValid = false;
//                 return;
//             }

//             window.LY_getPayData = function (actionId) {
//                 var foundItem = null;
//                 for (var i = 0; i < payArray.length; i++) {
//                     if (payArray[i].action_id === actionId) {
//                         foundItem = Object.assign({}, payArray[i]);
//                         foundItem.value = "";
//                         break;
//                     }
//                 }

//                 if (foundItem && foundItem.action_id) {
//                     if (Common.Common.shifou_tiaoshi_zhifu()) {
//                         foundItem.price = 100;
//                     }
//                     return foundItem;
//                 } else {
//                     console.log("========未找到叮叮当===", foundItem);
//                     return null;
//                 }
//             };

//             window.cpp_call_by_lost = function () {};
//         } else {
//             console.log("----不存在当前渠道配置，关闭支付----", platformName);
//         }
//     };

//     PayConfig.prototype.huoqu_zhifu_xinxi = function () {
//         return this.payInfo;
//     };

//     PayConfig.prototype.huoqu_zhifu_xinxi_by_id = function (id) {
//         return this.payInfo[id];
//     };

//     PayConfig.prototype.huoqu_zhifu_xinxi_by_action_id = function (actionId) {
//         for (var id in this.payInfo) {
//             if (this.payInfo[id].action_id === actionId) {
//                 return this.payInfo[id];
//             }
//         }
//         return null;
//     };

//     PayConfig.prototype.huoqu_zhifu_shifou_youxiao = function () {
//         if (!this.payValid) {
//             return false;
//         }

//         if (!Common.Common.isDebug()) {
//             if (cc.sys.os === cc.sys.OS_IOS) {
//                 return ailin_sdk_manager.instance.shifou_dakai_zhifu_ios === 1;
//             } else {
//                 return ailin_sdk_manager.shifouZhifuGoumai();
//             }
//         }

//         return true;
//     };

//     PayConfig.prototype.huoqu_zhifu_shifou_keyi = function (orderId) {
//         if (!this.huoqu_zhifu_shifou_youxiao() || !this.payInfo[orderId]) {
//             console.log("----不存在当前支付配置----orderId:" + orderId);
//             return false;
//         }
//         return true;
//     };

//     PayConfig.prototype.zhixing_zhifu = function (orderId) {
//         if (!this.huoqu_zhifu_shifou_keyi(orderId)) {
//             return Promise.resolve(null);
//         }
//         var payDetails = this.payInfo[orderId];
//         AL_APP_SDK_MANAGER.showLoading({ timeout: 5 });
//         console.log("----发起支付----action_id:" + payDetails.action_id 
//             + " orderId:" + payDetails.id + ",price:" + payDetails.price);
//         return new Promise(function (resolve) {
//             ailin_sdk_manager.diaoyongZhifuJiekou(Number(payDetails.action_id), function (result) {
//                 console.log("----支付结果----", result);
//                 if (result) {
//                     PayData.PayData.getInstance().yichu_guanggao();
//                     AL_APP_SDK_MANAGER.emit(Define.GameEvent.ON_PAY_SUCCESS, payDetails);
//                     resolve(payDetails);
//                 } else {
//                     resolve(null);
//                 }
//                 AL_APP_SDK_MANAGER.hideLoading();
//             });
//         });
//     };

//     PayConfig.prototype.jiancha_dingdan = function () {
//         var self = this;
//         return new Promise(function (resolve) {
//             if (!self.huoqu_zhifu_shifou_youxiao()) {
//                 resolve(null);
//                 return;
//             }
//             ailin_sdk_manager.huoquYonghuDingdanLiebiao({
//                 callFun: function (orderData) {
//                     console.log("----获取订单----", orderData);
//                     if (!orderData.data) {
//                         console.error("----获取订单失败----");
//                         resolve(null);
//                         return;
//                     }
//                     console.log("异常的订单:", orderData.data);
//                     var emailInstance = EmailData.EmailData.getInstance();
//                     var emailList = emailInstance.huoqu_suoyou_youjian();
//                     orderData.data.forEach(function (order) {
//                         if (!emailList.some(email => email.id === order.orderSn)) {
//                             emailInstance.tianjia_youjian({
//                                 id: order.orderSn,
//                                 actionId: order.actionId,
//                                 type: 1,
//                                 createTime: Common.Common.huoqu_dangqian_shijian(),
//                                 title: i18nMgr.i18nMgr._huoqu_biaoqian("补单标题"),
//                                 validTime: 60
//                             });
//                         }
//                     });
//                     resolve(orderData.data);
//                 }
//             });
//         });
//     };

//     PayConfig.instance = null;
//     return PayConfig;
// })(require("./AL_GAME_HUA_ConfigHandler").ConfigHandler);

// exports.PayConfig = PayConfig;



const AL_GAME_HUA_i18nMgr = require("./AL_GAME_HUA_i18nMgr");
const AL_GAME_HUA_Common = require("./AL_GAME_HUA_Common");
const AL_GAME_HUA_Define = require("./AL_GAME_HUA_Define");
const AL_GAME_HUA_EmailData = require("./AL_GAME_HUA_EmailData");
const AL_GAME_HUA_PayData = require("./AL_GAME_HUA_PayData");
const { ConfigHandler } = require("./AL_GAME_HUA_ConfigHandler");

/**
 * 支付配置类，用于管理支付相关配置和逻辑
 */
class PayConfig extends ConfigHandler {
    static instance = null;

    /**
     * 获取单例实例
     * @returns {PayConfig} PayConfig 实例
     */
    static getInstance() {
        if (!this.instance) {
            this.instance = new PayConfig();
        }
        return this.instance;
    }

    /**
     * 销毁单例实例
     */
    destroy() {
        PayConfig.instance = null;
    }

    /**
     * 加载支付配置
     * @returns {Promise<void>}
     */
    async jiazai_peizhi() {
        const config = await this.yibu_jiazai_json_peizhi("Pay");
        if (config) {
            this.payConf = {};
            this.payInfo = {};
            for (const platformKey in config) {
                const platformConfig = config[platformKey];
                this.payConf[platformKey] = [];

                for (let i = 0; i < platformConfig.length; i++) {
                    const payItem = platformConfig[i];
                    const rewards = payItem.rewardType;
                    const parsedRewards = [];

                    for (let j = 0; j < rewards.length; j += 2) {
                        const goodsId = rewards[j];
                        const num = rewards[j + 1];
                        parsedRewards.push({ goodsId, num });
                    }

                    payItem.rewardType = parsedRewards;

                    this.payConf[platformKey].push({
                        id: payItem.id,
                        action_id: payItem.payId,
                        productName: payItem.name,
                        productDesc: payItem.desc,
                        price: 100 * payItem.price,
                        currency: payItem.currency,
                        count: 1,
                        value: [],
                        rewardType: payItem.rewardType
                    });
                }
            }
            this.chushihua_zhifu_shuju();
        }
    }

    /**
     * 初始化支付数据
     */
    chushihua_zhifu_shuju() {
        const platformName = ailin_sdk_manager.getSysPlatformName();
        const platformKey =
            platformName === "native"
                ? `native_${ailin_sdk_manager.huoquYuanshengXitongXiangguanXinxi()}`
                : platformName;

        this.payValid = platformKey in this.payConf;

        if (this.payValid) {
            const currentPlatformConfig = this.payConf[platformKey];
            window.lieyou_PayData = this.payConf;

            for (const payItem of currentPlatformConfig) {
                this.payInfo[payItem.id] = payItem;
            }

            if (!this.huoqu_zhifu_shifou_youxiao()) {
                console.log("----支付未开启----");
                this.payValid = false;
                return;
            }

            window.LY_getPayData = (actionId) => {
                for (const payItem of currentPlatformConfig) {
                    if (payItem.action_id === actionId) {
                        const result = { ...payItem, value: "" };
                        if (AL_GAME_HUA_Common.Common.shifou_tiaoshi_zhifu()) {
                            result.price = 100;
                        }
                        return result;
                    }
                }
                console.log("========未找到支付配置=====", actionId);
                return null;
            };

            window.cpp_call_by_lost = () => {};
        } else {
            console.log("----不存在当前渠道配置，关闭支付----", platformKey);
        }
    }

    /**
     * 获取支付信息
     * @returns {Object} 支付信息
     */
    huoqu_zhifu_xinxi() {
        return this.payInfo;
    }

    /**
     * 根据支付 ID 获取支付信息
     * @param {string} id 支付 ID
     * @returns {Object} 支付信息
     */
    huoqu_zhifu_xinxi_by_id(id) {
        return this.payInfo[id];
    }

    /**
     * 根据支付 Action ID 获取支付信息
     * @param {string} actionId 支付 Action ID
     * @returns {Object|null} 支付信息
     */
    huoqu_zhifu_xinxi_by_action_id(actionId) {
        for (const key in this.payInfo) {
            if (this.payInfo[key].action_id === actionId) {
                return this.payInfo[key];
            }
        }
        return null;
    }

    /**
     * 检查支付是否有效
     * @returns {boolean} 是否有效
     */
    huoqu_zhifu_shifou_youxiao() {
        return (
            this.payValid &&
            (AL_GAME_HUA_Common.Common.isDebug() ||
                (cc.sys.os === cc.sys.OS_IOS
                    ? ailin_sdk_manager.instance.shifou_dakai_zhifu_ios === 1
                    : ailin_sdk_manager.shifouZhifuGoumai()))
        );
    }

    /**
     * 检查是否允许支付
     * @param {string} orderId 订单 ID
     * @returns {boolean} 是否允许支付
     */
    huoqu_zhifu_shifou_keyi(orderId) {
        const valid = this.huoqu_zhifu_shifou_youxiao() && this.payInfo[orderId];
        if (!valid) {
            console.log("----不存在当前支付配置----orderId:", orderId);
        }
        return valid;
    }

    /**
     * 执行支付
     * @param {string} orderId 订单 ID
     * @returns {Promise<Object|null>} 支付结果
     */
    async zhixing_zhifu(orderId) {
        if (!this.huoqu_zhifu_shifou_keyi(orderId)) {
            return Promise.resolve(null);
        }

        const payItem = this.payInfo[orderId];
        AL_APP_SDK_MANAGER.showLoading({ timeout: 5 });

        console.log(`----发起支付----action_id:${payItem.action_id} orderId:${payItem.id}, price:${payItem.price}`);

        return new Promise((resolve) => {
            ailin_sdk_manager.diaoyongZhifuJiekou(Number(payItem.action_id), (result) => {
                console.log("----支付结果----", result);
                if (result) {
                    AL_GAME_HUA_PayData.PayData.getInstance().yichu_guanggao();
                    AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_PAY_SUCCESS, payItem);
                    resolve(payItem);
                } else {
                    resolve(null);
                }
                AL_APP_SDK_MANAGER.hideLoading();
            });
        });
    }

    /**
     * 检查订单
     * @returns {Promise<Array|null>} 订单数据
     */
    async jiancha_dingdan() {
        if (!this.huoqu_zhifu_shifou_youxiao()) {
            return Promise.resolve(null);
        }

        return new Promise((resolve) => {
            ailin_sdk_manager.huoquYonghuDingdanLiebiao({
                callFun: (response) => {
                    console.log("----获取订单----", response);
                    if (!response.data) {
                        console.error("----获取订单失败----");
                        resolve(null);
                        return;
                    }

                    console.log("异常的订单:", response.data);

                    const emailDataInstance = AL_GAME_HUA_EmailData.EmailData.getInstance();
                    const allEmails = emailDataInstance.huoqu_suoyou_youjian();

                    for (const order of response.data) {
                        const isOrderExist = allEmails.some((email) => email.id === order.orderSn);
                        if (!isOrderExist) {
                            emailDataInstance.tianjia_youjian({
                                id: order.orderSn,
                                actionId: order.actionId,
                                type: 1,
                                createTime: AL_GAME_HUA_Common.Common.huoqu_dangqian_shijian(),
                                title: AL_GAME_HUA_i18nMgr.i18nMgr._huoqu_biaoqian("补单标题"),
                                validTime: 60,
                            });
                        }
                    }

                    resolve(response.data);
                },
            });
        });
    }
}

exports.PayConfig = PayConfig;
